// import MapConfig from "./MapConfig";


import Config from "../../application/constants/Config";
import Const from "../../application/constants/Const";


export default class ParseSatelliteConfig {
    // public forceLoose: boolean;

    constructor(){
        // this.forceLoose = opt.forceLoose;
    }

    private parserTileOrigin(data) {
        let originNodeArray = data.childNodes;
        let tileOrigin = { leftX: -Const.Number.MERCATORANGE, leftY: Const.Number.MERCATORANGE };
        originNodeArray.forEach(element => {
            if (element.tagName == 'X') {
                tileOrigin.leftX = Number(element.childNodes[0].nodeValue);
            } else if (element.tagName == 'Y') {
                tileOrigin.leftY = Number(element.childNodes[0].nodeValue);
            }
        });

        Config.Map.DatumPoint = [tileOrigin.leftX, tileOrigin.leftY]
        // MapConfig.DatumPoint = [tileOrigin.leftX, tileOrigin.leftY];
    }

    private parserLodInfor(data) {
        let lodInfoChildren = data.childNodes;
        let lodInforMap: Map<number, { Scale: number, Resolution: number }> = new Map();
        lodInfoChildren.forEach(lodInfor => {
            let levelInfor = {};
            lodInfor.childNodes.forEach(element => {
                if (element.tagName == 'LevelID') {
                    levelInfor['LevelID'] = Number(element.childNodes[0].nodeValue);
                }
                if (element.tagName == 'Scale') {
                    levelInfor['Scale'] = Number(element.childNodes[0].nodeValue);
                }
                if (element.tagName == 'Resolution') {
                    levelInfor['Resolution'] = Number(element.childNodes[0].nodeValue);
                }
            });
            lodInforMap.set(levelInfor['LevelID'], { 'Scale': levelInfor['Scale'], 'Resolution': levelInfor['Resolution'] });
        });
        Config.Map.SatelliteConfigMap = lodInforMap;
        // MapConfig.SatelliteConfigMap = lodInforMap;
    }

    public loadJson(uri: string, callback?: Function) {
        var xhr = new XMLHttpRequest();
        let self = this;
        xhr.onload = function () {

            let parser = new DOMParser();
            let xmlDoc = parser.parseFromString(xhr.response, "text/xml");

            var CacheInfo = xmlDoc.getElementsByTagName('CacheInfo');
            CacheInfo[0].childNodes.forEach((element: any) => {
                if (element.tagName == 'TileCacheInfo') {
                    let childNode = element.childNodes;
                    childNode.forEach(element => {
                        if (element.tagName == 'TileOrigin') {
                            self.parserTileOrigin(element);
                        }
                        if (element.tagName == 'LODInfos') {
                            self.parserLodInfor(element);
                        }
                    });
                }else if (element.tagName == 'CacheStorageInfo') {
                    let childNode = element.childNodes;
                    childNode.forEach(element => {
                        if (element.tagName == 'StorageFormat') {
                            let value:string = element.childNodes[0].nodeValue;
                            if(value.indexOf('esriMapCacheStorageModeCompact') != -1 ){//紧凑型文件   //&& !self.forceLoose
                                Config.Map.SatelliteTileType = 1;
                                // MapConfig.SatelliteTileType = 1;
                            }else{ //松散型文件
                                Config.Map.SatelliteTileType = 0;
                                // MapConfig.SatelliteTileType = 0;
                            }
                        }
                    });
                }
            });
            if(callback){
                callback();
            }
        };

        xhr.open('GET', uri, true);
        xhr.send();

    }

}